package cz.data.domain.quality.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cz.data.common.base.BaseController;
import cz.data.common.base.SimpleDataResponse;
import cz.data.common.base.TableDataResponse;
import cz.data.common.core.JsonPage;
import cz.data.domain.quality.mapstruct.RuleLevelMapper;
import cz.data.domain.quality.model.entity.RuleLevelEntity;
import cz.data.domain.quality.model.query.RuleLevelQuery;
import cz.data.domain.quality.model.vo.RuleLevelVo;
import cz.data.domain.quality.service.RuleLevelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = {"规则级别"})
@RestController
@RequestMapping("/ruleLevels")
public class RuleLevelController extends BaseController {

    @Autowired
    private RuleLevelService ruleLevelService;

    @Autowired
    private RuleLevelMapper ruleLevelMapper;

    @ApiOperation(value = "规则级别-详情")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/{id}")
    public SimpleDataResponse<RuleLevelVo> getRuleLevelById(@PathVariable String id) {
        RuleLevelEntity ruleLevelEntity = ruleLevelService.getRuleLevelById(id);
        return new SimpleDataResponse<>(ruleLevelMapper.toVO(ruleLevelEntity));
    }

    @ApiOperation(value = "规则级别-获取列表")
    @GetMapping("/list")
    public TableDataResponse<RuleLevelEntity> getRuleTypeList() {
        List<RuleLevelEntity> list = ruleLevelService.list(Wrappers.emptyWrapper());
        return new TableDataResponse<>(list);
    }

    @ApiOperation(value = "规则级别-分页查询")
    @ApiImplicitParam(name = "ruleLevelQuery", value = "查询实体ruleLevelQuery", required = true, dataTypeClass = RuleLevelQuery.class)
    @GetMapping("/page")
    public SimpleDataResponse<JsonPage<RuleLevelVo>> getRuleLevelPage(RuleLevelQuery ruleLevelQuery) {
        QueryWrapper<RuleLevelEntity> queryWrapper = new QueryWrapper<>();
        IPage<RuleLevelEntity> page = ruleLevelService.page(new Page<>(ruleLevelQuery.getPageNum(), ruleLevelQuery.getPageSize()), queryWrapper);
        List<RuleLevelVo> collect = page.getRecords().stream().map(ruleLevelMapper::toVO).collect(Collectors.toList());
        JsonPage<RuleLevelVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
        return new SimpleDataResponse<>(jsonPage);
    }
}
